package com.puccampinas.finalproject.core.controller;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.puccampinas.finalproject.core.exceptions.NullUserException;
import com.puccampinas.finalproject.core.logs.LogUtils;
import com.puccampinas.finalproject.core.logs.LoggerType;
import com.puccampinas.finalproject.core.persistence.abstracts.ActivitiesDAO;
import com.puccampinas.finalproject.core.persistence.abstracts.UserDAO;
import com.puccampinas.finalproject.core.persistence.entity.Activity;
import com.puccampinas.finalproject.core.persistence.entity.Client;
import com.puccampinas.finalproject.core.persistence.entity.Message;
import com.puccampinas.finalproject.core.persistence.entity.Partner;

public class UserController {
	
	public static Boolean register(Client user) throws NullUserException {
		if (user != null) {
			UserDAO userDAO = UserDAO.getDAO();
			userDAO.insert(user);
		} else {
			throw new NullUserException();
		}
		return true;
	}
	
	public static Boolean update(Client user) {
		return true;
	}
	
	public static Boolean disable(Client user) throws NullUserException {
		if (user != null) {
			UserDAO userDAO = UserDAO.getDAO();
			userDAO.disable(user);
		} else {
			throw new NullUserException();
		}
		
		return true;
	}
	
	public static Boolean enable(Client user) throws NullUserException {
		if (user != null) {
			UserDAO userDAO = UserDAO.getDAO();
			userDAO.enable(user);
		} else {
			throw new NullUserException();
		}
		
		return true;
	}
	
	public static List<Partner> listPartners(String userEmail) {
		
		if (userEmail != null && !"".equals(userEmail)) {
			UserDAO userDAO = UserDAO.getDAO();
			return userDAO.findAll(userEmail);
		} 
		
		return null;
	}
	
	public static Client find(String userEmail) {
		if (userEmail != null && !"".equals(userEmail)) {
			UserDAO userDAO = UserDAO.getDAO();
			
			Client c = new Client();
			c.setEmail(userEmail);
			
			return userDAO.find(c);
		} 
		
		return null;
	}
	
	public static List<Activity> findAllActivities(Long partnerId) {
		
		if (partnerId != null) {
			ActivitiesDAO actDAO = ActivitiesDAO.getDAO();
			return actDAO.findAll(partnerId);
		}
		
		return null;
	}
	
	public static Boolean acompanhar(Long userId, Long partnerId) {
		if (userId != null && partnerId != null) {
			UserDAO userDAO = UserDAO.getDAO();
			try {
				return userDAO.acompanhar(userId, partnerId);
			} catch (SQLException ex) {
				LogUtils.error(LoggerType.EXCEPTIONS, "Erro ao tentar acompanhar parceiro");
			}
		}
		return false;
	}
	
	public static Boolean desacompanhar(Long userId, Long partnerId) {
		if (userId != null && partnerId != null) {
			UserDAO userDAO = UserDAO.getDAO();
			try {
				return userDAO.desacompanhar(userId, partnerId);
			} catch (SQLException ex) {
				LogUtils.error(LoggerType.EXCEPTIONS, "Erro ao tentar desacompanhar parceiro");
			}
		}
		return false;
	}
	
	public static Boolean salvarCoordenadas(Long userId, String latitude, String longitude) {
		if (userId != null && StringUtils.isNotEmpty(latitude) && StringUtils.isNotEmpty(longitude)) {
			UserDAO userDAO = UserDAO.getDAO();
			try {
				return userDAO.salvarCoordenada(userId, latitude, longitude);
			} catch (SQLException e) {
				LogUtils.error(LoggerType.EXCEPTIONS, "Erro ao tentar salvarCoordenadas");
			}
		}
		
		return false;
	}
	
	public static List<Message> obterMensagens(Long userId) {
		if (userId != null) {
			UserDAO userDAO = UserDAO.getDAO();
			return userDAO.obterMensagens(userId);
		}
		return null;
	}
}
